//
//  MyViewController.m
//  YiQiTingShu
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "MyViewController.h"
#import "WHNTableViewCell.h"
#import "WHNSuggestViewController.h"

@interface MyViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)UIImageView *imageView;


@end

@implementation MyViewController

-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view NightWithType:UIViewColorTypeNormal];
    self.title = @"我的";
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = self.imageView;
}
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49 - 10 * FitHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView NightWithType:UIViewColorTypeNormal];
    }
    return _tableView;
}
-(UIImageView *)imageView
{
    if (!_imageView) {
        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 * FitHeight)];
        _imageView.image = [UIImage imageNamed:@"my"];
    }
    return _imageView;
}
#pragma mark - 计算单个文件大小的方法
-(long long)fileSizeAtPath:(NSString *)path
{
    //创建一个文件管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:path]) {
        return [[manager attributesOfItemAtPath:path error:nil] fileSize];
    }
    return 0;
}
#pragma mark - 计算文件夹大小的方法
-(float)folderSizeAtPath:(NSString *)path
{
    //创建文件管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:path]) {
        return 0;
    }
    //根据路径获取文件夹里面的元素集合
    //获取集合类型的枚举器
    NSEnumerator *enumerator = [[manager subpathsAtPath:path] objectEnumerator];
    //每次遍历得到的文件名
    NSString *fileName = [NSString string];
    //文件夹大小
    float folderSize = 0;
    while ((fileName = [enumerator nextObject]) != nil) {
        NSString *absolutePath = [path stringByAppendingPathComponent:fileName];//字符串拼接时加/
        folderSize += [self fileSizeAtPath:absolutePath];
    }
    return folderSize / (1024.0 * 1024.0);
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        static NSString *identifier = @"night";
        WHNTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[WHNTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            cell.kind = WHNTableViewCellKindFive;
            cell.TitleLabel.text = @"夜间模式";
            [cell.swch addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventValueChanged];
        }
        return cell;
    }
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];

    }
    switch (indexPath.row) {
        case 1:
            cell.textLabel.text = @"意见反馈";
            break;
        case 2:{
            NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
            NSString *cachePath = [path stringByAppendingPathComponent:@"Caches"];
            cell.textLabel.text = @"清理缓存";
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fM",[self folderSizeAtPath:cachePath]];
            break;
        }
        case 3:
            cell.textLabel.text = @"关于我们";
            break;

        default:
            break;
    }
    [cell NightWithType:UIViewColorTypeNormal];
    [cell.textLabel NightWithType:UIViewColorTypeNormal];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60 * FitHeight;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 1:{
            WHNSuggestViewController *sugVC = [[WHNSuggestViewController alloc] init];
            self.tabBarController.tabBar.hidden = YES;
            [self.navigationController pushViewController:sugVC animated:YES];
            break;
        }
        case 2:{
            NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
            NSFileManager *manager = [NSFileManager defaultManager];
            if ([manager fileExistsAtPath:path]) {
                NSArray *fileNameArray = [manager subpathsAtPath:path];
                for (NSString *fileName in fileNameArray) {
                    NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
                    if ([manager removeItemAtPath:absolutePath error:nil]) {
                        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温心提示"message:@"缓存已被清理干净了哦~"preferredStyle:UIAlertControllerStyleAlert];
                        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
                        [alert addAction:defaultAction];
                        [self presentViewController:alert animated:YES completion:nil];
                    }
                }
            }
            [_tableView reloadData];
            break;
        }
            
        default:
            break;
    }
}

-(void)buttonAction
{
    [ThemeManage shareThemeManage].isNight = ![ThemeManage shareThemeManage].isNight;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:nil];
    [[NSUserDefaults standardUserDefaults] setBool:[ThemeManage shareThemeManage].isNight forKey:@"Night"];
//    if ([ThemeManage shareThemeManage].isNight) {
//        self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
//    }
//    else{
//        self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
//    }
}

@end
